// pages/service/service_status.js
const api = require("../request/api")
const request = require("../request/request")
const util = require("../utils/util")
const App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,

        barHeight: App.globalData.paddingTop,
        styledata: {
            "bg_color": "white",
            "color": "#000",
            "leftflag": true,
            // "homeflag": true,
            "name": "售后详情"
        },

        // 当前步骤
        nowStep: 1,

        // 商家收货信息
        sellerInfo: {
            name: '唐星',
            mobile: '13824817355',
            region: '广东省广州市海珠区会展东商务中心206'
        },

        // 物流信息弹出框
        codevisible: false,

        // 快递公司选择器
        deliveryText: '',
        deliveryValue: [],
        deliverys: [
            { label: '中通', value: '0' },
            { label: '圆通', value: '1' },
            { label: '顺丰', value: '2' },
            { label: '韵达', value: '3' },
            { label: '邮政', value: '4' },
        ],

        setpOne: [
            {
                title: '买家申请',
                content: '已向后台提出售后申请'
            },
            {
                title: '商家处理',
                content: '等待后台审核'
            }
        ],

        // ctype等于1表示申请退款
        ctype: -1,
        isService: false,

        // 退款状态 0申请中 1拒绝 2已退款
        pay_status: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let { oid, trade_num, real_pay } = options
        this.setData({
            uid: App.globalData.uid,
            oid,
            trade_no: trade_num,
            RMamount: real_pay
        })
        await this.checkIsServicing()
        await this.checkRefundment()
    },

    // 请求判断是否有售后记录，有则只显示售后记录按钮
    async checkIsServicing() {
        let oid = this.data.oid
        let res = await request.postall(api.suggestdetail, { oid })
        if (res.code == -1) return
        // 标识为售后中，步骤条全部灰暗
        this.setData({
            isService: true,
            ctype: res.data.ctype
        })
    },

    // 查询退款单详情 展示页面
    async checkRefundment() {
        let oid = this.data.oid
        let res = await request.postall(api.RMdetail, { oid })
        if (res.code == -1) return
        // 标识为售后中，步骤条全部灰暗
        this.setData({
            pay_status: res.data.pay_status
        })
        if (res.data.pay_status == 2) {
            let setpOne = this.data.setpOne
            setpOne.push({
                title: '售后结果',
                content: `退款成功，退款金额￥${res.data.amount}。请前往微信零钱查收`
            })
            this.setData({
                setpOne,
                nowStep: 3
            })
        }

    },

    submitcode() {
        util.showToast('提交物流信息')
        this.setData({
            codevisible: false,
        });
    },

    showDeliveryPicker() {
        this.setData({
            deliveryVisible: true,
        });
    },

    onColumnChange(e) {
        console.log('picker pick:', e);
    },

    onPickerChange(e) {
        console.log('确定');
        const { key } = e.currentTarget.dataset;
        const { value, label } = e.detail;

        console.log('picker change:', e.detail);
        this.setData({
            deliveryVisible: false,
            deliveryValue: value,
            deliveryText: label,
        });
    },

    onPickerCancel(e) {
        const { key } = e.currentTarget.dataset;
        console.log(e, '取消');
        console.log('picker1 cancel:');
        this.setData({
            deliveryVisible: false,
        });
    },

    // 物流填写框弹出
    codePopup(e) {
        this.setData(
            { codevisible: true }
        );
    },

    // 点击遮罩层去隐藏弹出层
    onVisibleChange(e) {
        this.setData({
            codevisible: e.detail.visible,
        });
    },

    // 撤销申请
    cancelapply() {
        let id = 'fdsfd'
        util.showModal('确认撤销', function test() {
            console.log(id);
        })
    },

    // 复制商家收货手机
    copymobile() {
        console.log(558);
        wx.setClipboardData({
            data: this.data.sellerInfo.mobile,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'success',
                    duration: 2000
                });
            },
            fail(err) {
                console.log(err);
            }
        });
    },

    // 复制商家收货地址
    copyregion() {
        wx.setClipboardData({
            data: this.data.sellerInfo.region,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'success',
                    duration: 2000
                });
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})